package it.myAndroid.ShadowSMS;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import android.util.Log;
import android.util.Base64;


/**
 * @author stefazzi
 * @date 19/feb/2013
 * @version 
 */
public class ShadowSMSStringCrypto 
{
	private static final String CIPHER_ALGORITHM = "AES";
	private static final String RANDOM_GENERATOR_ALGORITHM = "SHA1PRNG";
	private static final int RANDOM_KEY_SIZE = 128;
	
	public static String encrypt( String password, String data ) throws ShadowSMSCryptoException 
	{
		try
		{
		byte[] secretKey = generateKey( password.getBytes() );
	  byte[] clear = data.getBytes();
		
	  SecretKeySpec secretKeySpec = new SecretKeySpec( secretKey, CIPHER_ALGORITHM );
		Cipher cipher = Cipher.getInstance( CIPHER_ALGORITHM );
	  cipher.init( Cipher.ENCRYPT_MODE, secretKeySpec );
	    
	  byte[] encrypted = cipher.doFinal( clear );
	  String encryptedString = Base64.encodeToString( encrypted, Base64.DEFAULT );
	    
		return encryptedString;
		}
		catch(Exception e)
		{
			Log.e(ShadowSMSStringCrypto.class.getName(),"Error",e);
			throw new ShadowSMSCryptoException("Errore classe Crypto");
		}
	}
	

	public static String decrypt( String password, String encryptedData ) throws ShadowSMSCryptoException 
	{
		try
		{
			byte[] secretKey = generateKey( password.getBytes() );
		
			SecretKeySpec secretKeySpec = new SecretKeySpec( secretKey, CIPHER_ALGORITHM );
			Cipher cipher = Cipher.getInstance( CIPHER_ALGORITHM );
			cipher.init( Cipher.DECRYPT_MODE, secretKeySpec );
	    
			byte[] encrypted = Base64.decode( encryptedData, Base64.DEFAULT );
			byte[] decrypted = cipher.doFinal( encrypted );
	    
			return new String( decrypted );
		}
		catch(Exception e)
		{
			Log.e(ShadowSMSStringCrypto.class.getName(),"Error",e);
			throw new ShadowSMSCryptoException("Errore classe Crypto");
		}

	}
	
	
	public static byte[] generateKey( byte[] seed ) throws ShadowSMSCryptoException
	{
		try
		{
			KeyGenerator keyGenerator = KeyGenerator.getInstance( CIPHER_ALGORITHM );
			SecureRandom secureRandom = SecureRandom.getInstance( RANDOM_GENERATOR_ALGORITHM );
			secureRandom.setSeed( seed );
			keyGenerator.init( RANDOM_KEY_SIZE, secureRandom );
			SecretKey secretKey = keyGenerator.generateKey();
			return secretKey.getEncoded();
		}
	  catch(Exception e)
		{
			Log.e(ShadowSMSStringCrypto.class.getName(),"Error",e);
			throw new ShadowSMSCryptoException("Errore classe Crypto");
		}

	}
}
